package com.itheima.mall.controller.umsController;

import com.itheima.mall.commons.PageBean;
import com.itheima.mall.service.umsService.*;
import com.itheima.pojo.ums.UmsMenu;
import com.itheima.pojo.ums.UmsResource;
import com.itheima.pojo.ums.UmsRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleMenuRelationService umsRoleMenuRelationService;

    @Autowired
    private UmsRoleResourceRelationService umsRoleResourceRelationService;

    @Autowired
    private UmsRoleService umsRoleService;

    @Autowired
    private UmsMenuService umsMenuService;

    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 给角色分配菜单
     * @param menuIds 分配的菜单数组
     * @param roleId 角色ID
     * @return
     */
    @PostMapping("/allocMenu")
    public ResponseEntity allocMenu(ArrayList<Long> menuIds,Long roleId){
        umsRoleMenuRelationService.allocMenu(menuIds,roleId);
        return ResponseEntity.ok(null);
    }

    /**
     * 给角色分配资源
     * @param resourceIds 资源ID数组
     * @param roleId 角色ID
     * @return
     */
    @PostMapping("/allocResource")
    public ResponseEntity allocResource(ArrayList<Long> resourceIds,Long roleId){
        umsRoleResourceRelationService.allocResource(resourceIds,roleId);
        return ResponseEntity.ok(null);
    }

    /**
     * 添加角色
     * @param role 角色信息
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity create(@RequestBody UmsRole role){
        umsRoleService.create(role);
        return ResponseEntity.ok(null);
    }

    /**
     * 批量删除角色
     * @param ids 角色IDS数组
     * @return
     */
    @PostMapping("/delete")
    public ResponseEntity delete(ArrayList<Long> ids){
        umsRoleService.delete(ids);
        return ResponseEntity.ok(null);
    }

    /**
     * 根据角色名称分页获取角色列表
     * @param pageNum 页码
     * @param pageSize 条目数
     * @param keyword 角色关键字
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<PageBean<UmsRole>> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageNum",required = false,defaultValue = "10") Integer pageSize,
                                                   @RequestParam String keyword){
        PageBean<UmsRole> res =umsRoleService.list(pageNum,pageSize,keyword);
        return ResponseEntity.ok(res);
    }

    /**
     * 获取所有角色
     * @return
     */
    @GetMapping("/listAll")
    public ResponseEntity<ArrayList<UmsRole>> listAll(){
        ArrayList<UmsRole> res = umsRoleService.listAll();
        return ResponseEntity.ok(res);
    }

    /**
     *获取角色相关菜单
     * @param roleId 角色ID
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public ResponseEntity<ArrayList<UmsMenu>> listMenu(@PathVariable long roleId){
        //根据角色ID查询关联菜单IDS数组
        ArrayList<Long> menuIds = umsRoleMenuRelationService.getmenuIds(roleId);
        //根据关联菜单IDS数组查询菜单数组
        ArrayList<UmsMenu> res = umsMenuService.listMenu(menuIds);
        return ResponseEntity.ok(res);
    }

    /**
     * 获取角色相关资源
     * @param roleId 角色ID
     * @return
     */
    @GetMapping("listResource/{roleId}")
    public ResponseEntity<ArrayList<UmsResource>> listResource(@PathVariable long roleId){
        //根据角色ID查询关联资源IDS数组
        ArrayList<Long> resourceIds =umsRoleResourceRelationService.getresourceIds(roleId);
        //根据关联资源IDS数组查询资源数组
        ArrayList<UmsResource> res =umsResourceService.listResource(resourceIds);
        return ResponseEntity.ok(res);
    }

    /**
     *修改角色
     * @param id 角色ID
     * @param role 角色信息
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateById(@PathVariable Long id,@RequestBody UmsRole role){
        umsRoleService.updateById(id,role);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改角色状态
     * @param id 角色ID
     * @param status 需要修改的角色状态标识
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public ResponseEntity updateStatus(@PathVariable long id ,Integer status){
        umsRoleService.updateStatus(id,status);
        return ResponseEntity.ok(null);
    }
}
